package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TFormConfigMapper;
import com.ruoyi.system.domain.TFormConfig;
import com.ruoyi.system.service.ITFormConfigService;

/**
 * 表单配置Service业务层处理
 * 
 * @author xwj
 * @date 2024-03-30
 */
@Service
public class TFormConfigServiceImpl extends ServiceImpl<TFormConfigMapper, TFormConfig> implements ITFormConfigService
{
    @Autowired
    private TFormConfigMapper tFormConfigMapper;

    /**
     * 查询表单配置
     * 
     * @param id 表单配置主键
     * @return 表单配置
     */
    @Override
    public TFormConfig selectTFormConfigById(Long id)
    {
        return tFormConfigMapper.selectTFormConfigById(id);
    }

    /**
     * 查询表单配置列表
     * 
     * @param tFormConfig 表单配置
     * @return 表单配置
     */
    @Override
    public List<TFormConfig> selectTFormConfigList(TFormConfig tFormConfig)
    {
        return tFormConfigMapper.selectTFormConfigList(tFormConfig);
    }

    /**
     * 新增表单配置
     * 
     * @param tFormConfig 表单配置
     * @return 结果
     */
    @Override
    public TFormConfig insertTFormConfig(TFormConfig tFormConfig, LoginUser loginUser)
    {
        TFormConfig exist = selectOne("table_name", tFormConfig.getTableName());
        if(exist != null) {
            exist.setTableForm(tFormConfig.getTableForm());
            exist.setUpdateTime(new Date());
            exist.setUpdateBy(loginUser.getUsername());
            saveOrUpdate(exist);
        } else {
            tFormConfig.setCreateTime(new Date());
            tFormConfig.setCreateBy(loginUser.getUsername());
            save(tFormConfig);
        }

        return tFormConfig;
    }

    /**
     * 修改表单配置
     * 
     * @param tFormConfig 表单配置
     * @return 结果
     */
    @Override
    public int updateTFormConfig(TFormConfig tFormConfig)
    {
        return tFormConfigMapper.updateTFormConfig(tFormConfig);
    }

    /**
     * 批量删除表单配置
     * 
     * @param ids 需要删除的表单配置主键
     * @return 结果
     */
    @Override
    public int deleteTFormConfigByIds(Long[] ids)
    {
        return tFormConfigMapper.deleteTFormConfigByIds(ids);
    }

    /**
     * 删除表单配置信息
     * 
     * @param id 表单配置主键
     * @return 结果
     */
    @Override
    public int deleteTFormConfigById(Long id)
    {
        return tFormConfigMapper.deleteTFormConfigById(id);
    }

    @Override
    public TFormConfig getByTable(String tableName) {
        return selectOne("table_name", tableName);
    }
}
